package util

import (
	"bytes"
	_ "bytes"
	"crypto/md5"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	url2 "net/url"
)
func BuildClient() *http.Client {
	return &http.Client{}
}
//var baseApi = "https://ifanyi.iciba.com/index.php?c=trans&m=fy&client=6&auth_user=key_ciba"
func fy(w string) string {
	sum := md5.Sum([]byte("6key_cibaifanyicjbysdlove1" + w))
	sign := fmt.Sprintf("%x",sum )[0:16]
	url := fmt.Sprintf("https://ifanyi.iciba.com/index.php?c=trans&m=fy&client=6&auth_user=key_ciba&sign=%s" , sign)
	//fmt.Println(url)
	client := BuildClient()
	//data := struct {
	//	From string `json:"from"`
	//	To string `json:"to"`
	//	Q string `json:"q"`
	//}{
	//	From: "auto",
	//	To: "auto",
	//	Q: w,
	//}
	ss := fmt.Sprintf("&to=auto&from=auto&q=%s",url2.QueryEscape(w))
	//json, _ := json.Marshal(&data)

	request, err := http.NewRequest("POST", url, bytes.NewReader([]byte(ss)))
	if err != nil {
		return "error"
	}
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	request.Header.Set("User-Agent",
		"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36 Edg/91.0.864.48 ")
	do, err := client.Do(request)
	if err!=nil {
		return "error"
	}
	defer do.Body.Close()
	all, err := ioutil.ReadAll(do.Body)
	if err!=nil {
		return "error"
	}
	resp := struct {
		Content struct{
			Out string `json:"out"`
		} `json:"content"`
	}{

	}
	json.Unmarshal(all,&resp)
	return resp.Content.Out
}

//var tranUrl   = "https://cn.bing.com/ttranslatev3?isVertical=1&&IG=D912A6F4B375402097B120244ABD8905&IID=translator.5023.13"
//func isChinese(w string) bool{
//	for _,ch  := range w {
//		if ch>='\u4e00' && ch<='\u9fff' {
//			return true
//		}
//	}
//	return false
//}
//func ToZh( en string) (string,error) {
//
//
//	str := "&fromLang=auto-detect&text=bad%20%20&to=zh-Hans&token=PXpE11PlNJDOwWxHo3gUTaaU_Q-CX1yD&key=1632671390671"/*fmt.Sprintf("&fromLang=%s&to=%s&text=%s","auto-detect","zh",en)*/
//	//en 转中文
//	post, err := http.Post(tranUrl,
//		"application/x-www-form-urlencoded",
//		bytes.NewBuffer( []byte(str)))
//
//	if err!=nil {
//		return "", err
//	}
//	defer post.Body.Close()
//	all, err := ioutil.ReadAll(post.Body)
//	if err!=nil {
//		return "", err
//	}
//	return string(all),nil
//}
//
//func ToEn( en string) (string,error) {
//
//	//fmt.Println(string(strbytes))
//	str := "&fromLang=auto-detect&text=bad%20%20&to=zh-Hans&token=PXpE11PlNJDOwWxHo3gUTaaU_Q-CX1yD&key=1632671390671"/*fmt.Sprintf("&from=%s&to=%s&text=%s","zh","en",en)*/
//	//if err!=nil {
//	//	log.Printf("errorInfo := %v",err)
//	//	return "", err
//	//}
//	//en 转中文
//	post, err := http.Post(tranUrl, "application/x-www-form-urlencoded", bytes.NewBuffer( []byte(str) ))
//	if err!=nil {
//		return "", err
//	}
//	defer post.Body.Close()
//	all, err := ioutil.ReadAll(post.Body)
//	if err!=nil {
//		return "", err
//	}
//	return string(all),nil
//}

func ParseSentence(sentence string ) (string,error){
	s:= fy(sentence)
	return s,nil
}
